package com.nand.module.function.ui;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.base.utils.SpUtils;
import com.nand.module.function.R;
import com.nand.module.function.bean.EqDetailInfoBean;
import com.nand.module.function.bean.IdBean;
import com.nand.module.function.bean.SupplierBran;
import com.nand.module.function.constant.URL;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.utils.ToolUtils;
import com.nand.module.function.view.CommonPopupWindow;
import com.nand.module.function.view.WheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * created by 86185
 * on 2020/2/26
 */
@Route(path = "/function/clamping/compile")
public class ClampingCompileActivity extends BaseActivity implements View.OnClickListener {

    private String equipID;
    private String equipName;
    private String fixtureCode;
    private String fFixtureOid;
    private String equipOid;
    private int userId;
    private int type;
    private String userOid;
    private TextView mTvConfirm;
    private TextView mTvConfirm2;
    private int code;
    private String fSupplierOid;
    private String repairOid;
    private String debugOid;
    private String fixtureName;
    private TextView mTvGzbm,mTvGzmc,mTvRight,mTvRight2,mTvLeft,mTvLeft2,mTvLeft3;
    private LinearLayout mLl1,mLl2,mLl3;
    private ImageView mIvRight,mIvRight2;
    private EditText mEtRight;
    private View viewLine,viewLine2,viewLine3;
    private CommonPopupWindow window;
    private WheelView mWheelview2;
    private String repairer;
    private String repairSupplierCode;
    private LinearLayout mLlgys;
    private TextView mTvgys;
    private TextView mTvNbwx;


    @Override
    protected int initLayout() {
        return R.layout.activity_clamping_compile;
    }

    @Override
    protected void initView() {
        userId = SpUtils.readInt(ClampingCompileActivity.this, "userId", 0);
        code = getIntent().getIntExtra("code", 1);
        type = getIntent().getIntExtra("type", 0);
        fFixtureOid = getIntent().getStringExtra("fFixtureOid");
        fSupplierOid = getIntent().getStringExtra("fSupplierOid");
        fixtureCode = getIntent().getStringExtra("fixtureCode");
        fixtureName = getIntent().getStringExtra("fixtureName");


        mTvGzbm = findViewById(R.id.tv_gzbm);
        mTvGzmc = findViewById(R.id.tv_gzmc);
        mLl1 = findViewById(R.id.ll1);
        mLl2 = findViewById(R.id.ll2);
        mLl3 = findViewById(R.id.ll3);
        mTvLeft = findViewById(R.id.tv_left);
        mTvLeft2 = findViewById(R.id.tv_left2);
        mTvLeft3 = findViewById(R.id.tv_left3);
        mTvRight = findViewById(R.id.tv_right);
        mTvRight2 = findViewById(R.id.tv_right2);
        mIvRight = findViewById(R.id.iv_right);
        mIvRight2 = findViewById(R.id.iv_right2);
        mEtRight = findViewById(R.id.et_right);
        viewLine = findViewById(R.id.view_line);
        viewLine2 = findViewById(R.id.view_line2);
        viewLine3 = findViewById(R.id.view_line3);
        mLlgys = findViewById(R.id.ll_gys);
        mTvgys = findViewById(R.id.tv_gys);
        mTvNbwx = findViewById(R.id.tv_nbwx);

        mTvgys.setOnClickListener(this);
        mTvNbwx.setOnClickListener(this);
        mTvConfirm =findViewById(R.id.tv_confirm);
        mTvConfirm2=findViewById(R.id.tv_confirm2);


        mTvGzbm.setText(fixtureCode);
        mTvGzmc.setText(fixtureName);
        switch (type) {
            case 0:
                mTvConfirm.setText("保存");
                mTvConfirm2.setText("取消");
                findViewById(R.id.tv_confirm).setOnClickListener(this);
                findViewById(R.id.tv_confirm2).setOnClickListener(this);
                switch (code){
                    case 1:
                        equipID = getIntent().getStringExtra("equipID");

                        mLl1.setVisibility(View.VISIBLE);
                        mLl2.setVisibility(View.VISIBLE);
                        viewLine.setVisibility(View.VISIBLE);
                        viewLine2.setVisibility(View.VISIBLE);
                        mTvLeft.setText("设备编码：");
                        mTvLeft2.setText("设备名称：");


                        mTvRight.setText(equipID);

                        HttpParams params1 = new HttpParams();
                        params1.put("equipID", equipID);
                        OkGoUtils.getInstance().post(ClampingCompileActivity.this, URL.GET_DETAIL_INFO, params1, 1, false, new MyCallback() {
                            @Override
                            public void onSuccess(ResponseBean response, int id) {
                                LogUtils.e("shuju============" + response.getDataInfo().toString());
                                String data = response.getDataInfo().toString();
                                EqDetailInfoBean bean = JSON.parseObject(data, EqDetailInfoBean.class);
                                equipName = bean.getEquipName();
                                mTvRight2.setText(equipName);
                                equipOid = bean.getOid();
                            }

                            @Override
                            public void onError(Throwable e, int id) {

                            }
                        });
                        break;
                    case 2:
                        mLl1.setVisibility(View.VISIBLE);
                        mLl2.setVisibility(View.VISIBLE);
                        viewLine.setVisibility(View.VISIBLE);
                        viewLine2.setVisibility(View.VISIBLE);
                        mTvLeft.setText("维修方：");
                        mTvLeft2.setText("供应商：");
                        mIvRight2.setVisibility(View.VISIBLE);
                        mLlgys.setVisibility(View.VISIBLE);
                        mLl1.setOnClickListener(this);
                        mLl2.setOnClickListener(this);
                        break;
                }

                break;
            case 1:
                mLl1.setVisibility(View.VISIBLE);
                mLl2.setVisibility(View.VISIBLE);
                mLl3.setVisibility(View.VISIBLE);
                viewLine.setVisibility(View.VISIBLE);
                viewLine2.setVisibility(View.VISIBLE);
                viewLine3.setVisibility(View.VISIBLE);
                mTvLeft.setText("设备编码：");
                mTvLeft2.setText("设备名称：");
                mTvLeft3.setText("已使用：");
                mEtRight.setHint("请输入已使用次数");
                String equipCode = getIntent().getStringExtra("equipCode");
                String equipName = getIntent().getStringExtra("equipName");
                mTvRight.setText(equipCode);
                mTvRight2.setText(equipName);
                mTvConfirm.setText("解绑");
                findViewById(R.id.tv_confirm).setOnClickListener(this);
                mTvConfirm2.setVisibility(View.GONE);
                userOid = getIntent().getStringExtra("userOid");
                break;
            case 2:
                repairOid = getIntent().getStringExtra("repairOid");
                repairer = getIntent().getStringExtra("repairer");
                repairSupplierCode = getIntent().getStringExtra("repairSupplierCode");
                LogUtils.e("ac============="+repairOid);
                LogUtils.e("ac============="+repairer);
                LogUtils.e("ac============="+repairSupplierCode);
                mLl1.setVisibility(View.VISIBLE);

                mLl3.setVisibility(View.VISIBLE);
                viewLine.setVisibility(View.VISIBLE);

                viewLine3.setVisibility(View.VISIBLE);
                mTvLeft.setText("维修方：");
                mTvLeft2.setText("供应商：");
                mTvLeft3.setText("维修内容：");

                switch (repairer){
                    case "0":
                        mLl2.setVisibility(View.VISIBLE);
                        viewLine2.setVisibility(View.VISIBLE);
                        mTvRight.setText("供应商");
                        break;
                    case "1":
                        mTvRight.setText("内部维修");
                        mLl2.setVisibility(View.GONE);
                        viewLine2.setVisibility(View.GONE);
                        break;

                }

                mTvConfirm.setText("保存");
                mTvConfirm2.setText("取消");
                findViewById(R.id.tv_confirm).setOnClickListener(this);
                findViewById(R.id.tv_confirm2).setOnClickListener(this);
                break;
            case 3:
                mLl3.setVisibility(View.VISIBLE);
                mLl1.setVisibility(View.GONE);
                viewLine.setVisibility(View.VISIBLE);
                mTvLeft3.setText("调试内容：");
                mEtRight.setHint("请输入调试内容");
                debugOid = getIntent().getStringExtra("debugOid");
                mTvConfirm.setText("保存");
                mTvConfirm2.setText("取消");
                findViewById(R.id.tv_confirm).setOnClickListener(this);
                findViewById(R.id.tv_confirm2).setOnClickListener(this);
        }
    }

    @Override
    protected void initData() {

        OkGoUtils.getInstance().post(this, URL.QUERY_SUPPLIER, null, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (response.getCode() == 200) {
                    String data = response.getDataInfo().toString();
                    LogUtils.e("data====="+data);
                    SupplierBran bean = JSON.parseObject(data, SupplierBran.class);
                    List<SupplierBran.DataListBean> dataList = bean.getDataList();
                            for (int i = 0; i < dataList.size(); i++) {
                                IdBean idBean = new IdBean();
                                int i1 = Integer.parseInt(dataList.get(i).getOid());
                                idBean.setId(i1);
                                idBean.setType(dataList.get(i).getSupplierName());
                                gysList.add(idBean);

                                switch (type) {
                                    case 2:
                                        if (repairSupplierCode!= null && repairSupplierCode.equals(dataList.get(i).getSupplierCode())){
                                            mTvRight2.setText(dataList.get(i).getSupplierName());
                                        }
                                        break;
                                }
                            }

                    initPop();
                } else {
                    showToast(response.getMsg());
                }

            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });
    }

    private static final int REQUEST_CODE2 = 1005;

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_confirm) {
            HttpParams param = new HttpParams();
            switch (type) {
                case 0:

                    if (1 == code) {
                        param.put("fFixtureOid", fFixtureOid);
                        param.put("fEquipmentOid", equipOid);
                        param.put("creator", userId);
                        OkGoUtils.getInstance().post(this, URL.FIXTURE_FIXTURE, param, 1, true, new MyCallback() {
                            @Override
                            public void onSuccess(ResponseBean response, int id) {
                                if (200 == response.getCode()) {
                                    Intent intent = new Intent();
                                    setResult(RESULT_OK, intent);
                                    showToast("安装成功");
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Throwable e, int id) {

                            }
                        });
                    }else if (2 == code){
                       /* if (1001 == wxfID){
                            showToast("请选择维修方");
                            return;
                        }
*/
                        if (1001 == gysID && 0 == wxfID){
                            showToast("请选择供应商");
                            return;
                        }
                        if (0 == wxfID){
                            param.put("fSupplierOid", gysID);
                        }
                        param.put("fFixtureOid", fFixtureOid);
                        param.put("repairer", wxfID);
                        param.put("creator", userId);
                        OkGoUtils.getInstance().post(this, URL.FIXTURE_REPAIR, param, 1, true, new MyCallback() {
                            @Override
                            public void onSuccess(ResponseBean response, int id) {
                                if (200 == response.getCode()) {
                                    Intent intent = new Intent();
                                    setResult(RESULT_OK, intent);
                                    showToast("报修成功");
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Throwable e, int id) {

                            }
                        });
                    }else if (3 == code){
                    }
                    break;
                case 1: //安装解绑
                    String etRight = mEtRight.getText().toString();

                    if (etRight.isEmpty()) {
                        showToast("请输入已使用次数");
                        return;
                    }

                    LogUtils.e("a========="+userOid);
                    LogUtils.e("a========="+fFixtureOid);
                    LogUtils.e("a========="+etRight);
                    LogUtils.e("a========="+userId);
                    param.put("oid", userOid);
                    param.put("fFixtureOid", fFixtureOid);
                    param.put("usage", etRight);
                    param.put("updater", userId);
                    OkGoUtils.getInstance().post(this, URL.FIXTURE_UNFIXTURE, param, 1, true, new MyCallback() {
                        @Override
                        public void onSuccess(ResponseBean response, int id) {
                            if (200 == response.getCode()) {
                                Intent intent = new Intent();
                                setResult(RESULT_OK, intent);
                                showToast("解绑成功");
                                finish();
                            }
                        }

                        @Override
                        public void onError(Throwable e, int id) {

                        }
                    });
                    break;
                case 2:
                    String etRight2 = mEtRight.getText().toString();

                    LogUtils.e("repairOid="+repairOid);
                    param.put("oid", repairOid);
                    param.put("fFixtureOid", fFixtureOid);
                    param.put("content", etRight2); //维修内容
                    param.put("updater", userId);
                    OkGoUtils.getInstance().post(this, URL.UN_FIXTURE_REPAIR, param, 1, true, new MyCallback() {
                        @Override
                        public void onSuccess(ResponseBean response, int id) {
                            if (200 == response.getCode()) {
                                Intent intent = new Intent();
                                setResult(RESULT_OK, intent);
                                showToast("退库成功");
                                finish();
                            }
                        }

                        @Override
                        public void onError(Throwable e, int id) {

                        }
                    });
                    break;
                case 3: //调试退库
                    String etRight3 = mEtRight.getText().toString();

                    param.put("oid", debugOid);
                    param.put("fFixtureOid", fFixtureOid);
                    param.put("content", etRight3);
                    param.put("updater", userId);
                    OkGoUtils.getInstance().post(this, URL.FIXTURE_UN_DEBUG, param, 1, true, new MyCallback() {
                        @Override
                        public void onSuccess(ResponseBean response, int id) {
                            if (200 == response.getCode()) {
                                Intent intent = new Intent();
                                setResult(RESULT_OK, intent);
                                showToast("退库成功");
                                finish();
                            }
                        }

                        @Override
                        public void onError(Throwable e, int id) {

                        }
                    });
                    break;
            }

        } else if (id == R.id.tv_confirm2) {
            LogUtils.e("requestCode============");
            finish();
        }else if (id == R.id.ll2){
            if (gysList.size() == 0){
                showToast("没有供应商");
                return;
            }
            Type = 1;
            mWheelview2.setVisibility(View.VISIBLE);

            window.showAtLocation(mLl1, Gravity.BOTTOM, 0, 0);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 0.3f;
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            getWindow().setAttributes(lp);
        }else if (id == R.id.tv_gys){
            wxfID = 0;
            mTvgys.setBackgroundResource(R.drawable.m_border_13_blue);
            mTvNbwx.setBackgroundResource(R.drawable.m_border_13_99);
            mLl2.setVisibility(View.VISIBLE);
            viewLine2.setVisibility(View.VISIBLE);
            mTvgys.setTextColor(getResources().getColor(R.color.blue2));
            mTvNbwx.setTextColor(getResources().getColor(R.color.color_99));
        }else if (id == R.id.tv_nbwx){
            wxfID = 1;
            mTvNbwx.setBackgroundResource(R.drawable.m_border_13_blue);
            mTvgys.setBackgroundResource(R.drawable.m_border_13_99);
            mLl2.setVisibility(View.GONE);
            viewLine2.setVisibility(View.GONE);
            mTvgys.setTextColor(getResources().getColor(R.color.color_99));
            mTvNbwx.setTextColor(getResources().getColor(R.color.blue2));
        }
    }
    ArrayList<IdBean> gysList = new ArrayList<>();
    private String item = "";
    private int goWheel = 0;
    private int goWheel2 = 0;
    private int gysID = 1001;
    private int wxfID = 0;
    private int Type = 0;
    private void initPop() {

        window = new CommonPopupWindow(this, R.layout.popup_item_select, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
            @Override
            protected void initView() {
                View view = getContentView();
                mWheelview2 = view.findViewById(R.id.wheelview);
                TextView mTvConfirm = view.findViewById(R.id.tv_confirm);
                TextView mTvCancel = view.findViewById(R.id.tv_cancel);

                mWheelview2.setOffset(1);
                mWheelview2.setItems(gysList);
                mWheelview2.setOnWheelViewListener(new com.nand.module.function.view.WheelView.OnWheelViewListener() {
                    @Override
                    public void onSelected(int selectedIndex, IdBean ite) {
                        goWheel2 = 1;
                        item = ite.getType();
                        gysID = ite.getId();

                    }
                });


                mTvConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (0 == goWheel2){
                            gysID = gysList.get(0).getId();
                            mTvRight2.setText(gysList.get(0).getType());
                        }else {
                            mTvRight2.setText(item);
                        }
                        mTvRight2.setTextColor(getResources().getColor(R.color.color_66));

                        window.getPopupWindow().dismiss();
                    }
                });

                mTvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        window.getPopupWindow().dismiss();
                    }
                });
            }

            @Override
            protected void initEvent() {
            }

            @Override
            protected void initWindow() {
                super.initWindow();
                PopupWindow instance = getPopupWindow();
                instance.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.alpha = 1.0f;
                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                        getWindow().setAttributes(lp);
                    }
                });
            }
        };
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            //当isShouldHideInput(v, ev)为true时，表示的是点击输入框区域，则需要显示键盘，同时显示光标，反之，需要隐藏键盘、光标
            if (ToolUtils.isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    //处理Editext的光标隐藏、显示逻辑
                    mEtRight.clearFocus();
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
}
